//
// C++ Implementation: clocktower
//
// Description: 
//
//
// Author:  <>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "clocktower.h"
#include <libwormhole/snapshot.h>
#include "snapshotmanager.h"
#include <QDebug>
ClockTower::ClockTower(QObject *parent, SnapshotManager *sm)
 : QObject(parent)
{
	m_timer=new QTimer(this);
	m_timer->setSingleShot(true);
	m_snapshotmanager=sm;

	connect(m_timer, SIGNAL(timeout()), this, SLOT(slotTimeout()));
}


ClockTower::~ClockTower()
{
}

void ClockTower::registerSnapshot(Snapshot *s)
{

      sortedInsert(s);
updateTimer();

      /*m_timer->stop();
      SnapshotList tmpList;
      QPair<SnapshotList, QDateTime> tmppair;
      int i;
      qDebug() << s->snapshotschedule()->count()*60;
      QDateTime tmpDate=QDateTime::currentDateTime().addSecs(s->snapshotschedule()->count()*60);
      qDebug() << tmpDate;
      //check if already exists an item with same datetime
      for(i=0; i<m_snapshotlist.size(); i++)
	if(m_snapshotlist.at(i).second==tmpDate){
	    tmpList=m_snapshotlist.at(i).first;
	    tmpList << s;
	    break;
	}
      
      if(tmpList.isEmpty()){
	tmpList << s;
	tmppair.first=tmpList;
	tmppair.second=tmpDate;
	if(m_snapshotlist.isEmpty())
	  m_snapshotlist << tmppair;
	else
	  for(i=0; i<m_snapshotlist.size(); i++)
	  {
	    if(tmppair.second<=m_snapshotlist.at(i).second)
	      m_snapshotlist.insert(i, tmppair);
	  }

      }


qDebug() << QDateTime::currentDateTime().secsTo(m_snapshotlist.first().second);
    m_timer->setInterval(1000*QDateTime::currentDateTime().secsTo(m_snapshotlist.first().second));
     m_timer->start();*/
      //m_snapshotmanager->executeSnapshot(s);
}

bool ClockTower::unregisterSnapshot(Snapshot *s)
{
	for(int i=0; i<m_snapshotlist.size(); i++)
	{
		SnapshotList sublist=m_snapshotlist.at(i).first;
		if(sublist.contains(s))
		{
			sublist.removeAll(s);
			
			if(sublist.empty())
				m_snapshotlist.removeAt(i);
		}
	}
}

void ClockTower::slotTimeout()
{
	QPair<SnapshotList, QDateTime> pair=m_snapshotlist.takeFirst();
	qDebug() << "tick" <<pair.second;
	/*
		TODO: call snapshot function, update m_snapshotlist and set next delay
	*/
qDebug() << "size: " << m_snapshotlist.size();
	SnapshotList slist=pair.first;
	for(int i=0; i<slist.size(); i++)
	  m_snapshotmanager->executeSnapshot(slist.at(i));

       for(int i=0; i<slist.size(); i++)
	  sortedInsert(slist.at(i));

	updateTimer();
}

void ClockTower::sortedInsert(Snapshot *s)
{
     qDebug() << "size: " << m_snapshotlist.size();
      SnapshotList tmpList;
      QPair<SnapshotList, QDateTime> tmppair;
      int i;
      qDebug() << s->name() << s->snapshotschedule()->count()*60;
      QDateTime tmpDate=QDateTime::currentDateTime().addSecs(s->snapshotschedule()->count()*60);
      qDebug() << tmpDate;
      //check if already exists an item with same datetime
      for(i=0; i<m_snapshotlist.size(); i++)
	if(m_snapshotlist.at(i).second==tmpDate){
	    tmpList=m_snapshotlist.at(i).first;
	    tmpList << s;
	    break;
	}
      
      if(tmpList.isEmpty()){
	tmpList << s;
	tmppair.first=tmpList;
	tmppair.second=tmpDate;

	if(m_snapshotlist.isEmpty()){
	  m_snapshotlist << tmppair;
	  return;
	}
	

	  for(int j=0; j<m_snapshotlist.size(); j++)
	  {
	    if(tmppair.second<=m_snapshotlist.at(j).second){
	      m_snapshotlist.insert(j, tmppair);
	      return;
	    }
	  }

	 m_snapshotlist << tmppair;
	  
      }



}

void ClockTower::updateTimer()
{
m_timer->stop();
qDebug() << QDateTime::currentDateTime().secsTo(m_snapshotlist.first().second);
    m_timer->setInterval(1000*QDateTime::currentDateTime().secsTo(m_snapshotlist.first().second));
m_timer->start();
}
//#include "clocktower.moc"
#include "moc_clocktower.moc"
